package com.example.home.findWork.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.home.R;
import com.example.home.findWork.bean.ProfessionBean;

import java.util.List;

public class WordNameAdapter extends RecyclerView.Adapter<WordNameAdapter.MyHoldy> {
    private Context context;
    private List<ProfessionBean.RowsBean> list;
    public static final int DEFAULT_ID = 15;
    public interface OnItemClickListener {
        void onItemClick(String itemName,int itemId);
    }

    private OnItemClickListener mListener;

    public WordNameAdapter(Context context, List<ProfessionBean.RowsBean> list, OnItemClickListener listener) {
        this.context = context;
        this.list = list;
        this.mListener = listener;
    }


    @NonNull
    @Override
    public WordNameAdapter.MyHoldy onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_workname, null);
        return new WordNameAdapter.MyHoldy(view);
    }

    @Override
    public void onBindViewHolder(@NonNull WordNameAdapter.MyHoldy holder, int position) {
        String itemName = list.get(position).getProfessionName();
        holder.name.setText(position == list.size() - 1 ? "所有职位" : itemName);
    }



    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class MyHoldy extends RecyclerView.ViewHolder {
        private TextView name;

        public MyHoldy(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);

            // 设置点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION && mListener != null) {
                        mListener.onItemClick(list.get(position).getProfessionName(), list.get(position).getId());
                    }
                }
            });
        }
    }
}
